﻿using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Creator;
using Game.Server.PlayerEvent;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using Game.Server.Event;

namespace Game.Server.Plugins
{
    public class ChatRecordZHPlugin : ServerPlugin, IMessageEventHandle
    {
        public byte FirstLevel => 0;

        private string m_recordMessage = "";

        private string m_recordLastMessage = "";
        public override int Version => 10000;

        public override string Name => "聊天记录插件";

        public static string ChatRecordPath;

        public override void Initialize()
        {
            MessageEventManager.AddObject(this);
        }

        public override void Save()
        {
            if (m_recordMessage == string.Empty)
            {
                return;
            }
            using (FileStream fileStream = File.Open(ChatRecordPath, FileMode.OpenOrCreate))
            {
                fileStream.Position = fileStream.Length;
                using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                {
                    streamWriter.WriteLine(m_recordMessage);
                }
            }
            m_recordMessage = string.Empty;
        }

        public override void Load()
        {
            ChatRecordPath = GetPluginDataByWorldPath("ChatRecord.txt");
        }

        public string GetPluginDataByWorldPath(string fileName)
        {
            string pluginDataPath = Storage.GetSystemPath("app:/PluginData");
            if (!Directory.Exists(pluginDataPath))
            {
                Directory.CreateDirectory(pluginDataPath);
            }
            string worldName = GameManager.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.Name;
            string dataByWorldPath = Storage.CombinePaths(pluginDataPath, worldName);
            if (!Directory.Exists(dataByWorldPath))
            {
                Directory.CreateDirectory(dataByWorldPath);
            }
            return Storage.CombinePaths(dataByWorldPath, fileName);
        }

        public void ReceiveMessage(string playerName, NetNode netNode, Client From, string message, byte type, out bool External)
        {
            string typeStr = "";
            switch (type)
            {
                case 0: typeStr = "公"; break;
                case 1: typeStr = "队"; break;
                case 2: typeStr = "私"; break;
            }
            string mes = $"[{DateTime.Now.ToString("yy-MM-dd HH:mm:ss")}][{typeStr}] {playerName}:{message}";
            if (message.StartsWith("/"))
            {
                External = false;
                return;
            }
            if (mes != m_recordLastMessage)
            {
                m_recordLastMessage = mes;
                m_recordMessage += mes + "\n";
            }
            External = true;
        }

        public bool EditSignMessage(Point3 point, ComponentPlayer componentPlayer)
        {
            return true;
        }
    }
}